package com.zxp.pstationapk.view;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.zxp.pstationapk.R;
import com.zxp.pstationapk.base.Constant;
import com.zxp.pstationapk.util.SpUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Creaetd by zhangxianpeng
 */
public class MainActivity extends AppCompatActivity {
    @BindView(R.id.topbar_main)
    QMUITopBar topbarMain;
    @BindView(R.id.rv_pstation_110kv)
    RecyclerView rv_pstation_110kv;
    @BindView(R.id.rv_pstation_10kv)
    RecyclerView rv_pstation_10kv;
    @BindView(R.id.rv_pstation_35kv)
    RecyclerView rv_pstation_35kv;
    @BindView(R.id.tv_1)
    TextView tv_1;
    @BindView(R.id.tv_2)
    TextView tv_2;
    @BindView(R.id.tv_3)
    TextView tv_3;
    private String[] string_tv = {"110kV", "35kV", "10kV"};

    /**
     * 请求访问外部存储
     */
    private static final int READ_EXTERNAL_STORAGE_REQUEST_CODE = 103;
    /**
     * 请求写入外部存储
     */
    private static final int WRITE_EXTERNAL_STORAGE_REQUEST_CODE = 104;

    FullyLinearLayoutManager mLayoutManager = new FullyLinearLayoutManager(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        QMUIStatusBarHelper.translucent(this);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        topbarMain.setTitle(getResources().getString(R.string.tv_maintitle));
        tv_1.setText(string_tv[0]);
        tv_2.setText(string_tv[1]);
        tv_3.setText(string_tv[2]);
        topbarMain.setBackgroundAlpha(130);
        topbarMain.addRightImageButton(R.mipmap.image_more, R.id.topbar_right_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle(R.string.dialog_title).setMessage(R.string.dialog_message).setPositiveButton(R.string.dialog_pbtn, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                    }
                });

                builder.setNegativeButton(R.string.dialog_nbtn, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        Toast.makeText(MainActivity.this, R.string.tv_toast_3, Toast.LENGTH_SHORT).show();
                        SpUtils.clear(MainActivity.this);
                        Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                        startActivity(intent);
                        finish();
                    }
                });
                builder.show();
            }
        });

        final List<String> pstationData_110kv = new ArrayList<String>();
        final List<String> pstationData_35kv = new ArrayList<String>();
        final List<String> pstationData_10kv = new ArrayList<String>();

        for (String s : Constant.pstion_Data_110kv) {
            pstationData_110kv.add(s);
        }
        rv_pstation_110kv.setLayoutManager(new GridLayoutManager(this, 2));
        final MainAdapter mainAdapter = new MainAdapter(pstationData_110kv);
        rv_pstation_110kv.setAdapter(mainAdapter);
        rv_pstation_110kv.setNestedScrollingEnabled(false);
        rv_pstation_110kv.setItemAnimator(new DefaultItemAnimator());
        rv_pstation_110kv.addItemDecoration(new DividerGridItemDecoration(this));

        for (String s : Constant.pstion_Data_35kv) {
            pstationData_35kv.add(s);
        }
        rv_pstation_35kv.setLayoutManager(new GridLayoutManager(this, 2));
        final MainAdapter mainAdapter1 = new MainAdapter(pstationData_35kv);
        rv_pstation_35kv.setAdapter(mainAdapter1);
        rv_pstation_35kv.setNestedScrollingEnabled(false);
        rv_pstation_35kv.setItemAnimator(new DefaultItemAnimator());
        rv_pstation_35kv.addItemDecoration(new DividerGridItemDecoration(this));

        for (String s : Constant.pstion_Data_10kv) {
            pstationData_10kv.add(s);
        }
        rv_pstation_10kv.setLayoutManager(new GridLayoutManager(this, 2));
        final MainAdapter mainAdapter2 = new MainAdapter(pstationData_10kv);
        rv_pstation_10kv.setAdapter(mainAdapter2);
        rv_pstation_10kv.setNestedScrollingEnabled(false);
        rv_pstation_10kv.setItemAnimator(new DefaultItemAnimator());
        rv_pstation_10kv.addItemDecoration(new DividerGridItemDecoration(this));
    }

    public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {
        private List<String> mDatas;

        public MainAdapter(List<String> mDatas) {
            this.mDatas = mDatas;
        }

        @Override
        public MainAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_item_main, parent, false);
            ViewHolder viewHolder = new ViewHolder(view);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(MainAdapter.ViewHolder holder, final int position) {
            holder.tv_pstation_namee.setText(mDatas.get(position));
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String stationTitle = mDatas.get(position);
                    Intent intent = new Intent(MainActivity.this, RouteActivity.class);
                    intent.putExtra("stationTitle", stationTitle);
                    startActivity(intent);
                }
            });
        }

        @Override
        public int getItemCount() {
            return mDatas.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            TextView tv_pstation_namee;
            public ViewHolder(View itemView) {
                super(itemView);
                tv_pstation_namee = (TextView) itemView.findViewById(R.id.tv_pstation_namee);
            }
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        getPermission();
    }

    /**
     * 动态获取读写权限
     */
    public void getPermission() {
        //=======版本判断，当手机系统大于23时，才有必要判断权限是否获取
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            //=======获取读权限
            if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, READ_EXTERNAL_STORAGE_REQUEST_CODE);
            }

            //=======获取写权限
            if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, WRITE_EXTERNAL_STORAGE_REQUEST_CODE);
            }
        }

    }

    /**
     * 权限申请结果回调
     *
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 103:   //读
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // 已经获取对应权限
                } else {
                }
                break;
            case 104:   //写
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                } else {
                }
                break;
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
    }
}
